package com.project.xsf.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.xsf.entity.*;
import com.project.xsf.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mProceduring")
public class MProceduringController {
    @Autowired
    private MProceduringService mproceduringService;
    @Autowired
    private MProcedureModulingService mProcedureModulingService;
    @Autowired
    private MProcedureService mProcedureService;
    @Autowired
    private MProcedureModuleService mProcedureModuleService;
    @Autowired
    private MManufactureService mManufactureService;
    @PostMapping("/addMProceduring.action")
    public int addMProceduring(String mProceduringStr,String MProcedureModulingArrsStr,Integer mProcedureId){
        MProceduring  mProceduring = JSONObject.parseObject(mProceduringStr, MProceduring.class);
        MProcedureModuling[] mProcedureModulings = JSONObject.parseObject(MProcedureModulingArrsStr, MProcedureModuling[].class);
//            登记次数每次加1
//        System.out.println(mProceduring);
        mproceduringService.insertSelective(mProceduring);
        //修改实际数量和成本
        MProcedure mProcedure = mProcedureService.selectByPrimaryKey(mProcedureId);

        Double realModuleSubtotal=0.0;

        MManufacture mManufacture = mManufactureService.selectByPrimaryKey(mProcedure.getParentId());

        List<MProcedureModule> mProcedureModules = mProcedureModuleService.queryMProcedureModuleByMManufactureId(mManufacture.getId());


        for(int i=0;i<mProcedureModulings.length;i++){
            MProcedureModuling mProcedureModuling  = mProcedureModulings[i];
            //获取到MDesignModuleId
            Integer id = mProcedureModuling.getId();
            MProcedureModule mProcedureModule = mProcedureModuleService.selectByPrimaryKey(id);
            //修改实际使用数量和实际物料成本。。。
            mProcedureModule.setRealAmount(mProcedureModuling.getAmount()+mProcedureModule.getRealAmount());
            mProcedureModule.setRealSubtotal(mProcedureModuling.getSubtotal()+mProcedureModule.getRealSubtotal());
            for(int j=0;j<mProcedureModules.size();j++){
                MProcedureModule mProcedureModule1 = mProcedureModules.get(j);
                if(mProcedureModule1.getProductId().equals(mProcedureModule.getProductId())){
                    mProcedureModule1.setRealAmount(mProcedureModule.getRealAmount());
                }
                mProcedureModuleService.updateByPrimaryKeySelective(mProcedureModule1);
            }
            realModuleSubtotal+=mProcedureModuling.getSubtotal();
            mProcedureModuleService.updateByPrimaryKeySelective(mProcedureModule);

            mProcedureModuling.setId(null);
            mProcedureModuling.setParentId(mProceduring.getId());

            mProcedureModulingService.insertSelective(mProcedureModuling);
//            System.out.println(mProcedureModuling);
        }
        //工时成本小计
        mProcedure.setRealSubtotal(mProceduring.getSubtotal()+mProcedure.getRealSubtotal());
        //物料成本小计
        mProcedure.setRealModuleSubtotal(realModuleSubtotal+mProcedure.getRealModuleSubtotal());
        //工时数
        mProcedure.setRealLabourHourAmount(mProceduring.getLabourHourAmount()+mProcedure.getRealLabourHourAmount());
        //工序完成标志：已完成
        mProcedure.setProcedureFinishTag("G004-1");

        mProcedureService.updateByPrimaryKeySelective(mProcedure);

        //生产过程标志：S002-1（未完工）
        mManufacture.setManufactureProcedureTag("S002-1");
        return mManufactureService.updateByPrimaryKeySelective(mManufacture);
    }
    @PostMapping("/checkMProceduring.action")
    public void checkMProceduring(String mProcedureObjStr,String mProceduringObj){
        MProcedure mProcedure = JSONObject.parseObject(mProcedureObjStr, MProcedure.class);
        MProceduring mProceduring1 = JSONObject.parseObject(mProceduringObj, MProceduring.class);

        MProceduring mProceduring = mproceduringService.queryMProceduringByMProcedure(mProcedure.getParentId(), mProcedure.getProcedureId());
        mProceduring.setChecker(mProceduring1.getChecker());
        mProceduring.setCheckTime(mProceduring1.getCheckTime());
        mproceduringService.updateByPrimaryKeySelective(mProceduring);

        List<MProcedureModuling> mProcedureModulings = mProcedureModulingService.queryMProcedureModulingByPId(mProceduring.getId());
        Double realModuleSubtotal=0.0;
        for(int i=0;i<mProcedureModulings.size();i++){
            realModuleSubtotal+=mProcedureModulings.get(i).getSubtotal();
        }

        //修改实际数量和成本
        //工时成本小计
        mProcedure.setRealSubtotal(mProceduring.getSubtotal());
        //物料成本小计
        mProcedure.setRealModuleSubtotal(realModuleSubtotal);
        //工时数
        mProcedure.setRealLabourHourAmount(mProceduring.getLabourHourAmount());
        //工序完成标志：已审核
        mProcedure.setProcedureFinishTag("G004-3");
        mProcedureService.updateByPrimaryKeySelective(mProcedure);
    }
    @PostMapping("/queryMProceduringByMManufactureIdAndMProcedureId.action")
    public MProceduring queryMProceduringByMManufactureIdAndMProcedureId(String procedureId,Integer mProcedureId){
        MProceduring mProceduring = mproceduringService.queryMProceduringByMProcedure(mProcedureId, procedureId);
        return mProceduring;

    }
}
